﻿using Commons.Requests;
using Commons.ReturnData;
using OpenAI.Domain.Entities;
using OpenAI.Domain.IRepositories;
using OpenAI.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenAI.Infrastructure.Services
{
    public class ChatMessageService 
    {
        private readonly IChatMessageRepository repository;
        private readonly IChatSessionRepository chatSessionRepository;

        public ChatMessageService(IChatMessageRepository repository, IChatSessionRepository chatSessionRepository)
        {
            this.repository = repository;
            this.chatSessionRepository = chatSessionRepository;
        }

        public async Task<SingleResult> DeleteChatMessageAsync(int id, Guid appUserId)
        {
            return await repository.DeleteChatMessageAsync(id, appUserId);
        }

        public async Task<DataResult<ChatMessage>> GetChatMessagesAsync(PagingRequest pagingRequest,int cid)
        {
            return await repository.GetChatMessagesAsync(pagingRequest.PageSize,cid);
        }

        public async Task<SingleResult> SendChatMessageAsync(int cid, string content, Guid appUserId,bool isBot)
        {
            var chatMessage = new ChatMessage(isBot, content);
            chatMessage.ChatSession = await chatSessionRepository.GetChatSessionsByIdAsync(cid);
            if(chatMessage.ChatSession != null && chatMessage.ChatSession.AppUserId == appUserId) 
            {
                return await repository.SendChatMessageAsync(chatMessage);
            }
            else
            {
                return new SingleResult(false);
            }
        }
    }
}
